package com.gl.doc.operation.comment.bindcomment;

import com.gl.doc.builder.WordDocument;
import com.gl.doc.model.Paragraph;
import com.gl.doc.model.SubParagraph;
import com.gl.doc.model.WordComment;
import com.gl.doc.operation.comment.entity.TargetSentenceIndex;
import com.gl.doc.operation.comment.entity.TargetSubParagraphContentIndex;
import com.gl.doc.operation.comment.entity.WordCommentRelation;
import com.gl.doc.operation.comment.impl.DefaultBindComment;
import org.dom4j.Element;
import org.dom4j.tree.DefaultElement;

import java.io.File;
import java.util.List;

/**
 * 第一种段落里面只有一个子段落条件判断
 */
public class CaseOneBindComment extends DefaultBindComment {
    @Override
    public void doAddParagraphComment(WordCommentRelation wordCommentRelation) {
        List<TargetSubParagraphContentIndex> targetSubParagraphContentIndexList = wordCommentRelation.getTargetSubParagraphContentIndexList();
        if (targetSubParagraphContentIndexList.size() == 1) {
            //获取段落
            WordComment wordComment = wordCommentRelation.getWordComment();
            Paragraph targetParagraph = wordComment.getTargetParagraph();
            Element element = targetParagraph.getElement();
            List<Element> elements = element.elements();
            WordDocument wordDocument = wordCommentRelation.getWordDocument();
            //获取子段落
            TargetSubParagraphContentIndex targetSubParagraphContentIndex = targetSubParagraphContentIndexList.get(0);
            SubParagraph subParagraph = targetSubParagraphContentIndex.getSubParagraph();
            TargetSentenceIndex targetSentenceIndex = wordCommentRelation.getTargetSentenceIndex();
            Integer targetStartIndex = targetSubParagraphContentIndex.getStartIndex();
            Integer targetEndIndex = targetSubParagraphContentIndex.getEndIndex();
            Integer startIndex = targetSentenceIndex.getStartIndex();
            Integer endIndex = targetSentenceIndex.getEndIndex();
            File commentFile = new File(wordDocument.getWord().getCommentsPath());
            //加批注的第一个字的位置和段落的第一个字的一样并且最后一个字和段落最后一个子一样就是整段都是批注
            if (startIndex.equals(targetStartIndex) && endIndex.equals(targetEndIndex)) {//开始一样，结束一样
                super.addComment(wordComment, commentFile);
                super.doBindComment(subParagraph.getRIndex(), subParagraph.getRIndex() + 2, subParagraph.getRIndex() + 3, elements, wordComment);
            } else if (targetStartIndex.equals(startIndex) && endIndex < targetEndIndex) {//开始一样，结束不一样
                this.refactorSubParagraph(targetParagraph, elements, subParagraph, startIndex, endIndex + 1, endIndex + 1, targetEndIndex + 1);
                super.addComment(wordComment, commentFile);
                super.doBindComment(subParagraph.getRIndex(), subParagraph.getRIndex() + 2, subParagraph.getRIndex() + 3, elements, wordComment);
            } else if (startIndex > targetStartIndex && endIndex.equals(targetEndIndex)) {//开始不一样，结束一样
                this.refactorSubParagraph(targetParagraph, elements, subParagraph, targetStartIndex, startIndex, startIndex, endIndex + 1);
                super.addComment(wordComment, commentFile);
                super.doBindComment(subParagraph.getRIndex() + 1, subParagraph.getRIndex() + 3, subParagraph.getRIndex() + 4, elements, wordComment);
            } else if (startIndex > targetStartIndex && endIndex < targetEndIndex) {//大于开始，小于结束
                DefaultElement rElement = (DefaultElement) subParagraph.getRElement();
                String content = targetParagraph.getContent();
                //截取左边新增文字
                String leftContent = content.substring(targetStartIndex, startIndex);
                DefaultElement leftElement = (DefaultElement) rElement.clone();
                Element leftTElement = leftElement.element("t");
                leftTElement.setText(leftContent);
                elements.add(subParagraph.getRIndex(), leftElement);
                //截取中间新增文字
                String centerContent = content.substring(startIndex, endIndex + 1);
                DefaultElement centerElement = (DefaultElement) rElement.clone();
                Element centerTElement = centerElement.element("t");
                centerTElement.setText(centerContent);
                elements.add(subParagraph.getRIndex() + 1, centerElement);
                //设置右边新的文字段落
                String rightContent = content.substring(endIndex + 1, targetEndIndex + 1);
                Element rightTElement = subParagraph.getTElement();
                rightTElement.setText(rightContent);
                super.addComment(wordComment, commentFile);
                super.doBindComment(subParagraph.getRIndex() + 1, subParagraph.getRIndex() + 3, subParagraph.getRIndex() + 4, elements, wordComment);
            }
        }
    }

    /**
     * 重构段落内容
     *
     * @param paragraph       添加批注的段落
     * @param elements        添加批注段落的子段落xml元素集合
     * @param subParagraph    字段落
     * @param leftStartIndex  批注句子左边坐标开始坐标
     * @param leftEndIndex    批注句子左边坐标开始坐标
     * @param rightStartIndex 批注句子右边坐标开始坐标
     * @param rightEndIndex   批注句子右边坐标开始坐标
     */
    private void refactorSubParagraph(Paragraph paragraph, List<Element> elements, SubParagraph subParagraph, Integer leftStartIndex, Integer leftEndIndex, Integer rightStartIndex, Integer rightEndIndex) {
        //把左边的段落提取出来
        String content = paragraph.getContent();
        DefaultElement rElement = (DefaultElement) subParagraph.getRElement();
        DefaultElement leftElement = (DefaultElement) rElement.clone();
        Element leftTElement = leftElement.element("t");
        String leftContent = content.substring(leftStartIndex, leftEndIndex);
        leftTElement.setText(leftContent);
        elements.add(subParagraph.getRIndex(), leftElement);
        //设置右边新的文字段落
        String rightContent = content.substring(rightStartIndex, rightEndIndex);
        Element rightTElement = subParagraph.getTElement();
        rightTElement.setText(rightContent);
    }

}
